#include "gamedialogitem.h"
#include "gamedialog.h"
#include "player.h"
#include "gamedialogmanager.h"
#include "tileboarditems.h"
#include "QPainter"
#include "gametable.h"
#include <QFontDatabase>
#include <QPen>
#include <QStyleOption>
#include <QTimer>

//GameDialogItem
GameDialogItem::GameDialogItem(GameDialog* dialog)
    :QGraphicsObject(dialog), _dialog(dialog)
{
}

//GameDialogButton
GameDialogButton::GameDialogButton(GameDialog *d)
    :GameDialogItem(d), _label("")
{
    setAcceptHoverEvents(true);
    _bg = QPixmap();
}

void GameDialogButton::setLabel(QString s){
    _label = s;
    update();
}

void GameDialogButton::setPixmap(QPixmap p){
    _bg = p;
    update();
}

QRectF GameDialogButton::boundingRect() const{
    if(_bg.isNull())
        return QRectF(-WIDTH/2, -HEIGHT/2, WIDTH, HEIGHT);
    else
        return QRectF(-_bg.width()/2, -_bg.height()/2, _bg.width(), _bg.height());
}

void GameDialogButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    //bool over = option->state & QStyle::State_MouseOver;
    //painter->fillRect(boundingRect(), Qt::red);
    if(!_bg.isNull())
        painter->drawPixmap(-_bg.width()/2, -_bg.height()/2, _bg);
    painter->drawText(boundingRect(), Qt::AlignCenter, _label);
    //if(over)
        //painter->drawLine(QPointF(-WIDTH/2, HEIGHT/2), QPointF(WIDTH/2, HEIGHT/2));
}

void GameDialogButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    update();
}

void GameDialogButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event){
    update();
}

void GameDialogButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event){
    update();
}

//tao button
GameDialogTaoButton::GameDialogTaoButton(GameDialog *d, GSColor c)
    :GameDialogButton(d), _c(c)
{
    switch(_c)
    {
        case RED:
            setPixmap(QPixmap(":/Tao/images/shu.png"));
            break;
        case YELLOW:
            setPixmap(QPixmap(":/Tao/images/god.png"));
            break;
        case BLUE:
            setPixmap(QPixmap(":/Tao/images/wei.png"));
            break;
        case GREEN:
            setPixmap(QPixmap(":/Tao/images/wu.png"));
            break;
        case BLACK:
            setPixmap(QPixmap(":/Tao/images/qun.png"));
            break;
        case WHITE:
            break;
        default:
            break;
    }
}

void GameDialogTaoButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    ((GameTaoRequestDialog*)this->getDialog())->chooseColor(_c);
    emit pressed();
}

//head button
GameDialogHeadButton::GameDialogHeadButton(GameDialog *d, GSColor c)
    :GameDialogItem(d),_c(c)
{
    QList<Player*> pList = d->getGameTable()->getPlayerList();
    switch(_c)
    {
        case YELLOW:
            _headPix = QPixmap(":/playerHead/images/y_taoist.png");
            choosenPlayer = pList.at(0);
            break;
        case RED:
            _headPix = QPixmap(":/playerHead/images/r_taoist.png");
            choosenPlayer = pList.at(1);
            break;
        case GREEN:
            _headPix = QPixmap(":/playerHead/images/g_taoist.png");
            choosenPlayer = pList.at(2);
            break;
        case BLUE:
            _headPix = QPixmap(":/playerHead/images/b_taoist.png");
            choosenPlayer = pList.at(3);
            break;
        case BLACK:
            break;
        case WHITE:
            break;
        default:
            break;
    }
}


void GameDialogHeadButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    emit click(this);
    /*
    this->getDialog()->hideStarted();
    GameTileRequestDialog* diaRef = this->getDialog()->getDialogManager()->getTileRequestDialog();

    GameTable* currentTable = this->getDialog()->getGameTable();
    CenterTable* cTable = currentTable->getCenterTable();

    connect(cTable,SIGNAL(clicked(VillageTile*)),diaRef,SLOT(receiveClickOnTile(VillageTile*)));
    VillageTile* tile = this->getDialog()->getDialogManager()->askTileDialog();
    disconnect(cTable, SIGNAL(clicked(VillageTile*)), diaRef, SLOT(receiveClickOnTile(VillageTile*)));

    QPoint* clickedPos = tile->getPos();
    choosenPlayer->setPlayerPos(clickedPos);
    */
}

void GameDialogHeadButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->drawPixmap(-headWidth/2,-headHeight/2,headWidth,headHeight,_headPix);
}

QRectF GameDialogHeadButton::boundingRect() const
{
    return QRectF(-headWidth/2,-headHeight/2,headWidth,headHeight);
}

//dice
GameDialogDice::GameDialogDice(GameDialog *d)
    :GameDialogItem(d)
{
    side = qrand()%6;
    _colors << QPixmap(":/dice/images/dice_red.png")
            << QPixmap(":/dice/images/dice_yel.png") << QPixmap(":/dice/images/dice_blu.png")
                 << QPixmap(":/dice/images/dice_gre.png") << QPixmap(":/dice/images/dice_bla.png")
                 << QPixmap(":/dice/images/dice_whi.png") << QPixmap(":/dice/images/dice_int.png");
    rAnim = new QTimer(this);
    connect(rAnim, SIGNAL(timeout()), this, SLOT(roll()));
    rAnim->start(50);
}

GSColor GameDialogDice::getSideColor(){
    GSColor c;
    switch(side){
    case 0:
        c = RED;
        break;
    case 1:
        c = YELLOW;
        break;
    case 2:
        c = BLUE;
        break;
    case 3:
        c = GREEN;
        break;
    case 4:
        c = BLACK;
        break;
    case 5:
        c = WHITE;
        break;
    default:
        break;
    }
    return c;
}

QRectF GameDialogDice::boundingRect() const{
    return QRectF(-_colors[side].width()/2, -_colors[side].height()/2, _colors[side].width(), _colors[side].height());
}

void GameDialogDice::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->drawPixmap(-_colors[side].width()/2, -_colors[side].height()/2, _colors[side]);
}

bool GameDialogDice::isActive(){
    return rAnim->isActive();
}

void GameDialogDice::roll(){
    if(side==6){
        side = qrand()%6;
    }else{
        side = 6;
    }
    update();
}

void GameDialogDice::stop(){
    if(side==6)
        side = qrand()%6;
    update();
    rAnim->stop();
}

void GameDialogDice::mousePressEvent(QGraphicsSceneMouseEvent *event){

    rAnim->stop();
}

//dialog tips
DialogTips::DialogTips(GameDialog *d, QString t)
    :GameDialogItem(d),tipName(t)
{

}

QRectF DialogTips::boundingRect() const{
    return QRectF(-WIDTH/2, -HEIGHT/2, WIDTH, HEIGHT);
}

void DialogTips::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), QColor(255, 255, 255, 125));
    painter->setFont(QFont("Garamond", 12));
    painter->drawText(boundingRect(), Qt::AlignLeft | Qt::AlignVCenter, tipName);
}

//Class TaoItemButton
TaoItemButton::TaoItemButton(GameDialog *d,gs::GSColor c, Player* p)
    :GameDialogItem(d),_color(c),_pl(p)
{
    QFontDatabase::addApplicationFont( tr(":/font/font/CHILLER.TTF") );
    setItemPix(_color);
    setAcceptHoverEvents(true);
    this->setScale(1.0);
}

void TaoItemButton::setItemPix(gs::GSColor c)
{
    switch(c)
    {
        case RED:
            _taoPix = QPixmap(":/Tao/images/shu.png");
            break;
        case YELLOW:
            _taoPix = QPixmap(":/Tao/images/god.png");
            break;
        case BLUE:
            _taoPix = QPixmap(":/Tao/images/wei.png");
            break;
        case GREEN:
            _taoPix = QPixmap(":/Tao/images/wu.png");
            break;
        case BLACK:
            _taoPix = QPixmap(":/Tao/images/qun.png");
            break;
        case WHITE:
            break;
        default:
            break;
    }
}

void TaoItemButton::setPlayerTao(gs::GSColor c)
{
    switch(c)
    {
        case RED:
            _pl->resistChangeR(true);
            break;
        case YELLOW:
            _pl->resistChangeY(true);
            break;
        case BLUE:
            _pl->resistChangeBlue(true);
            break;
        case GREEN:
            _pl->resistChangeG(true);
            break;
        case BLACK:
            _pl->resistChangeBlack(true);
            break;
        case WHITE:
            break;
        default:
            break;
    }
}

QRectF TaoItemButton::boundingRect() const
{
    return QRectF(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
}

void TaoItemButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT,_taoPix);
}

void TaoItemButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    this->setScale(1.1);
    update();
}

void TaoItemButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    this->setScale(1.0);
    update();
}

void TaoItemButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    setPlayerTao(_color);
    update();
    this->getDialog()->hideStarted();
}
